package com.weichen.studentgameexchange.comment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.weichen.studentgameexchange.comment.dao.CommentReplyLikeMapper;
import com.weichen.studentgameexchange.comment.entity.CommentReply;
import com.weichen.studentgameexchange.comment.entity.CommentReplyLike;
import com.weichen.studentgameexchange.comment.entity.vo.ReplyLikeVo;
import com.weichen.studentgameexchange.comment.service.ICommentReplyLikeService;
import com.weichen.studentgameexchange.common.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 惟尘
 * @since 2025-02-05
 */
@Service
public class CommentReplyLikeServiceImpl extends ServiceImpl<CommentReplyLikeMapper, CommentReplyLike> implements ICommentReplyLikeService {

    @Autowired
    CommentReplyLikeMapper commentReplyLikeMapper;


    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<CommentReplyLike> adminPage(Integer page, Integer size) {
        Page<CommentReplyLike> commentReplyLikePage = new Page<>(page, size);
        LambdaQueryWrapper<CommentReplyLike> likeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        likeLambdaQueryWrapper.orderByDesc(CommentReplyLike::getLikeId);
        return commentReplyLikeMapper.selectPage(commentReplyLikePage, likeLambdaQueryWrapper);
    }

    @Override
    public Result userAdd(ReplyLikeVo replyLikeVo) {
        CommentReplyLike like = BeanUtil.copyProperties(replyLikeVo, CommentReplyLike.class);
        LambdaQueryWrapper<CommentReplyLike> likeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        likeLambdaQueryWrapper.eq(CommentReplyLike::getReplyId, like.getReplyId()).eq(CommentReplyLike::getUserId, like.getUserId());
        CommentReplyLike one = commentReplyLikeMapper.selectOne(likeLambdaQueryWrapper);
        if (one != null){
            userDelete(replyLikeVo);
            return Result.success("取消点赞");
        }
        commentReplyLikeMapper.insert(like);
        CommentReply reply = Db.lambdaQuery(CommentReply.class)
                .eq(CommentReply::getReplyId, like.getReplyId())
                .one();
        Db.lambdaUpdate(CommentReply.class)
                .eq(CommentReply::getReplyId,reply.getReplyId())
                .set(CommentReply::getLikeCount,reply.getLikeCount()+1)
                .update();
        return Result.success("点赞成功");
    }

    @Override
    public Result userDelete(ReplyLikeVo replyLikeVo) {
        CommentReplyLike like = BeanUtil.copyProperties(replyLikeVo, CommentReplyLike.class);
        LambdaQueryWrapper<CommentReplyLike> likeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        likeLambdaQueryWrapper.eq(CommentReplyLike::getReplyId, like.getReplyId()).eq(CommentReplyLike::getUserId, like.getUserId());
        CommentReplyLike one = commentReplyLikeMapper.selectOne(likeLambdaQueryWrapper);
        if (one == null)
            return Result.error("点赞数据不存在");
        commentReplyLikeMapper.deleteById(one.getLikeId());
        CommentReply reply = Db.lambdaQuery(CommentReply.class)
                .eq(CommentReply::getReplyId, like.getReplyId())
                .one();
        Db.lambdaUpdate(CommentReply.class)
                .eq(CommentReply::getReplyId,reply.getReplyId())
                .set(CommentReply::getLikeCount,reply.getLikeCount()-1)
                .update();
        return Result.success("取消点赞");
    }

    @Override
    public Result userselectCount(Long replyId) {
        CommentReply reply = Db.lambdaQuery(CommentReply.class)
                .eq(CommentReply::getReplyId, replyId)
                .one();
        Integer likeCount = reply.getLikeCount();
        return Result.success(likeCount);
    }

    @Override
    public Result userSelect(Long replyId,Long userId) {
        LambdaQueryWrapper<CommentReplyLike> likeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        likeLambdaQueryWrapper.eq(CommentReplyLike::getReplyId, replyId).eq(CommentReplyLike::getUserId, userId);
        CommentReplyLike one = commentReplyLikeMapper.selectOne(likeLambdaQueryWrapper);
        ReplyLikeVo vo = BeanUtil.copyProperties(one, ReplyLikeVo.class);
        if (vo != null)
            vo.setLiked(true);
        return Result.success(vo);
    }
}
